﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Motoball.screenmanager;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Motoball.Paddles;
using Microsoft.Xna.Framework.Input;

namespace Motoball.screens
{
    /// <summary>
    /// paddle-selection (1 of 4) and paddle-color selection
    /// </summary>
    public class PaddleSelectionScreen : OptionsSelectScreen
    {

        Color[] colors;
        int numberColors = 9;
        int currentColor = 0;

        int currentPlayerSelecting = 0;
        int[] paddleColorStorage;
        int[] paddlesSelected;
        bool[] enabledPaddles;
        bool singleplayer = false;

        public PaddleSelectionScreen(string screenName)
            : base(screenName)
        {
            enabledPaddles = new bool[4];
            paddleColorStorage = new int[2];
            paddlesSelected = new int[2];
            paddlesSelected[0] = 0;
            paddlesSelected[1] = 0;
            paddleColorStorage[0] = 0;
            paddleColorStorage[1] = 1;
            for (int i = 0; i < 4; i++)
                enabledPaddles[i] = true;
            colors = new Color[numberColors];
            colors[0] = Color.MidnightBlue;
            colors[1] = Color.Firebrick;
            colors[2] = Color.ForestGreen;
            colors[3] = Color.HotPink;
            colors[4] = Color.OliveDrab;
            colors[5] = Color.Turquoise;
            colors[6] = Color.White;
            colors[7] = Color.Black;
            colors[8] = Color.Orange;
        }

        #region Methods
 
        protected override void Exit()
        {
            //change player that is selecting
            if (toNextMenu)
            {
                currentPlayerSelecting++;
                if (currentPlayerSelecting > ContentStorage.numberPlayers)
                    currentPlayerSelecting = 0;
            }
            else
            {
                currentPlayerSelecting--;
                if (currentPlayerSelecting < 0)
                    currentPlayerSelecting = ContentStorage.numberPlayers;
            }
            base.Exit();
        }
        
        public override void LoadContent()
        {
            //memo:set appropiate items.selectable = false
            AddItem(new MenuPaddle(new BasicModel(screenManager.Content.Load<Model>("Models\\Beamer")), new BeamerAnimation()));
            //AddItem(new MenuPaddle(new BasicModel(screenManager.Content.Load<Model>("Models\\Roland")), new RolandAnimation()));
            AddItem(new MenuPaddle(new BasicModel(screenManager.Content.Load<Model>("Models\\Crusher")), new CrusherAnimation()));
            AddItem(new MenuPaddle(new BasicModel(screenManager.Content.Load<Model>("Models\\Racer")), new RacerAnimation()));

            PaddleAnimation.InitExhaustKeyFrames();
            items[2].scaleFactor /= 1.2f;
            foreach (MenuPaddle item in items)
                item.Initialize();
        }

        private void UpdateColors()
        {
            Vector3 color = new Vector3(colors[currentColor].R / 255f,
                                        colors[currentColor].G / 255f,
                                        colors[currentColor].B / 255f);
            foreach (MenuPaddle item in items)
                item.animation.UpdateColor(color, item.model);
        }

        public override void SetToActive()
        {
            currentColor = paddleColorStorage[currentPlayerSelecting];
            TopItem = paddlesSelected[currentPlayerSelecting];
            foreach (MenuPaddle item in items)
                item.ResetTransforms();
            UpdateColors();
            base.SetToActive();
        }
        protected override bool DoSomething()
        {
            if (enabledPaddles[TopItem])
            {
                paddlesSelected[currentPlayerSelecting] = TopItem;
                GameplayScreen gameplay = (GameplayScreen)screenManager.GetScreen("gameplayScreen");
                paddleColorStorage[currentPlayerSelecting] = currentColor;
                gameplay.SetPaddles(currentPlayerSelecting, TopItem, colors[currentColor]);
                toNextMenu = true;
                return true;
            }
            else
                return false;
        }

        //private void UpdateColBeamer()
        //{
        //    Vector3 color = new Vector3(colors[currentColor].R / 255f,
        //                                colors[currentColor].G / 255f,
        //                                colors[currentColor].B / 255f);

        //    items[0].model.diffColors["Obj_000002"] = color;
        //    items[0].model.diffColors["Kugel1"] = Vector3.Lerp(Vector3.One, color, 0.7f);
        //    items[0].model.diffColors["Kugel2"] = Vector3.Lerp(Vector3.One, color, 0.7f);

        //    items[0].model.diffColors["Rippe3"] = Vector3.Lerp(Vector3.One, color, 0.6f);
        //    items[0].model.diffColors["Rippe4"] = Vector3.Lerp(Vector3.One, color, 0.6f);

        //    items[0].model.diffColors["Rippe2"] = Vector3.Lerp(Vector3.One, color, 0.5f);
        //    items[0].model.diffColors["Rippe5"] = Vector3.Lerp(Vector3.One, color, 0.5f);

        //    items[0].model.diffColors["Rippe1"] = Vector3.Lerp(Vector3.One, color, 0.4f);
        //    items[0].model.diffColors["Rippe6"] = Vector3.Lerp(Vector3.One, color, 0.4f);

        //}
        //private void UpdateColCrusher()
        //{
        //    Vector3 color = new Vector3(colors[currentColor].R / 255f,
        //                                colors[currentColor].G / 255f,
        //                                colors[currentColor].B / 255f);

        //    items[2].model.diffColors["Crusher_Auspuff1"] = Vector3.Lerp(Vector3.Zero, color, 0.8f);
        //    items[2].model.diffColors["Crusher_Auspuff2"] = Vector3.Lerp(Vector3.Zero, color, 0.8f);

        //    items[2].model.diffColors["Crusher_Schild"] = Vector3.Lerp(Vector3.Zero, color, 0.6f);

        //    //engine
        //    items[2].model.diffColors["Obj_000001"] = Vector3.Lerp(Vector3.One, color, 0.5f);
        //    //left wheel-protector
        //    items[2].model.diffColors["Obj_000003"] = Vector3.Lerp(Vector3.Zero, color, 0.4f);
        //    //right wheel-protector
        //    items[2].model.diffColors["Obj_000004"] = Vector3.Lerp(Vector3.Zero, color, 0.4f);

        //    //Meshes:
        //    //Crusher_RadX
        //    //Crusher_AuspuffX
        //    //Crusher_Schild
        //    //Obj_00000X (1,3,4)
        //}
        //private void UpdateColRoland()
        //{
        //    Vector3 color = new Vector3(colors[currentColor].R / 255f,
        //                                colors[currentColor].G / 255f,
        //                                colors[currentColor].B / 255f);

        //    items[1].model.diffColors["Roland_Auspuff1"] = Vector3.Lerp(Vector3.One, color, 0.6f);
        //    items[1].model.diffColors["Roland_Auspuff2"] = Vector3.Lerp(Vector3.One, color, 0.6f);

        //    items[1].model.diffColors["Roland_Schild"] = Vector3.Lerp(Vector3.Zero, color, 0.6f);

        //    items[1].model.diffColors["Roland_Chassis"] = Vector3.Lerp(Vector3.One, color, 0.6f);

        //    //back-side gas-pipes
        //    items[1].model.diffColors["Obj_000003"] = Vector3.Lerp(Vector3.Zero, color, 0.5f);
        //    items[1].model.diffColors["Obj_000005"] = Vector3.Lerp(Vector3.Zero, color, 0.4f);

        //    //front-side gas-pipes
        //    items[1].model.diffColors["Obj_000004"] = Vector3.Lerp(Vector3.Zero, color, 0.4f);
        //    items[1].model.diffColors["Obj_000006"] = Vector3.Lerp(Vector3.Zero, color, 0.4f);

        //    //Meshes:
        //    //Roland_AuspuffX
        //    //Roland_Chassis
        //    //Roland_RadX
        //    //Obj_00000X (3-6)
        //    //Roland_Schild
        //}
        //private void UpdateColRacer()
        //{ }

        public override void HandleInput()
        {
            if (currentPlayerSelecting == 0)
            {
                if (GameScreen.screenManager.Input.IsNewButtonPress(Buttons.DPadUp, PlayerIndex.One)
                    || GameScreen.screenManager.Input.IsNewButtonPress(Buttons.LeftThumbstickUp, PlayerIndex.One))
                    
                    
                {
                    currentColor++;
                    if (currentColor >= numberColors)
                        currentColor = 0;
                    UpdateColors();
                }
            }
            else
            {
                if (GameScreen.screenManager.Input.IsNewButtonPress(Buttons.DPadUp, PlayerIndex.Two)
                    || GameScreen.screenManager.Input.IsNewButtonPress(Buttons.LeftThumbstickUp, PlayerIndex.Two))
                    
                    
                {
                    currentColor--;
                    if (currentColor < 0)
                        currentColor = numberColors - 1;
                    UpdateColors();
                }
            }
           
            base.HandleInput();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            MenuPaddle item = (MenuPaddle)items[TopItem];
            item.UpdateAnimation(gameTime);
        }
        #endregion

        #region Properties
        public bool Singleplayer
        {
            set { singleplayer = value; }
        }
        public int CurrentPlayerSelecting
        {
            set { currentPlayerSelecting = value; }
        }
       
        #endregion
    }
}
